/**
 * 
 */


var NotifyRecore = {
	init: function(){
		NotifyRecore.initEvent();
		this.bind();
	},
	initEvent: function(){
		var me = this;
		$('#p_notifyObj').on('change',function(){
			var notifyObj=$("#p_notifyObj").val();
			if(notifyObj==1){
				$("#notifyId").text('企业：');
				$("#p_notifyId").empty();
				me.bind();
			}else{
				$("#notifyId").text('司机：');
				$("#p_notifyId").empty();
				me.queryAllDriver();
			}
		});
		$("#btn").click(function(){
			me.saveNotifyRecore();
    	});
	},
	
	bind : function(){
		$("#p_notifyId").select2({
			theme: "default",
	        allowClear: true,
	        placeholder: "请输入企业名称查询",
	        ajax:{
	        	url : appCtx + "base/notifyRecore/queryAllEnterprise.html",
	            dataType:"json",
	            delay:250,
	            data:function(params){
	            	if(params.term == undefined) {
	            		params.term = "";
	            	}
	                return {
	                	name:params.term
	                };
	            },
	            cache:true,
	            processResults: function (res) {
			    	if(res){
			    		var options = [];
	    				$.each(res,function(index,obj) {
							var option = {"id":obj.id, "text":obj.name};
    	                    options.push(option);
	    				});
    	                return {
    	                	results: options,
    	                    pagination: {
    	                        more:false
    	                    }
    	                };
			    	}
	            },
	            escapeMarkup: function (markup) { return markup; },
	            minimumInputLength: 1
	        }
	    });
	},
	queryAllDriver : function(){
		$("#p_notifyId").select2({
			theme: "default",
	        allowClear: true,
	        placeholder: "请输入司机名称查询",
	        ajax:{
	        	url : appCtx + "base/notifyRecore/queryAllDriver.html",
	            dataType:"json",
	            delay:250,
	            cache:true,
	            data:function(params){
	            	if(params.term == undefined) {
	            		params.term = "";
	            	}
	                return {
	                	name:params.term
	                };
	            },
	            processResults: function (res) {
			    	if(res){
			    		var options = [];
	    				$.each(res,function(index,obj) {
							var option = {"id":obj.id, "text":obj.name};
    	                    options.push(option);
	    				});
    	                return {
    	                	results: options,
    	                    pagination: {
    	                        more:false
    	                    }
    	                };
			    	}
	            },
	            escapeMarkup: function (markup) { return markup; },
	            minimumInputLength: 1
	        }
	    });
	},
saveNotifyRecore:function(){
		if(!checkparam()){
			return;
		}
		var notifyObj=$("#p_notifyObj").val();
		var notifyId=$("#p_notifyId").val();
		var notifyType=$("#p_notifyType").val();
		var notifyCont=$("#p_notifyCont").val();
		$.ajax({
			type : 'post',
			url : appCtx + "base/notifyRecore/saveNotifyRecore.html",
			data : {
				notifyObj:notifyObj,
				notifyId:notifyId,
				notifyType:notifyType,
				notifyCont:notifyCont
			},
			cache : false,
			dataType : "JSON",
			success : function(data) {
				if (data.code == "0") {
					global.ajaxLoading.pop(data.msg,false,2000);
					$('#navbarModal').modal('hide');
				}else {
					global.ajaxLoading.pop(data.msg,false,2000);
					return;
				}
			}
		});
}
};
NotifyRecore.init();
/**
 * 
 */
function checkparam(){
	var notifyObj=$("#p_notifyObj").val();
	var notifyId=$("#p_notifyId").val();
	var notifyType=$("#p_notifyType").val();
	var notifyCont=$("#p_notifyCont").val();
	if(notifyId==null||notifyId==''){
		if(notifyObj==1){
			global.ajaxLoading.pop('请选择企业',false,1000);
		}else{
			global.ajaxLoading.pop('请选择司机',false,1000);
		}
		return false;
	}
	if(notifyCont==null||notifyCont==''){
		global.ajaxLoading.pop('请填写通知内容',false,1000);
		return false;
	}
	return true;
}

